package utils

import (
	"github.com/pquerna/otp"
	"github.com/pquerna/otp/totp"
)

var OTP = new(Otp)

type Otp struct{}

// 生成OTP密钥和二维码链接
func (o *Otp) GenerateOTPKey(appname, username string) (*otp.Key, error) {
	key, err := totp.Generate(totp.GenerateOpts{
		Issuer:      appname,
		AccountName: username,
		Algorithm:   otp.AlgorithmSHA256,
	})
	if err != nil {
		return nil, err
	}
	return key, nil
}

// 验证OTP码
func (o *Otp) VerifyOTP(secret, code string) bool {
	return totp.Validate(code, secret)
}
